<?php
class koudai_dz_logControl extends skymvc{
	public $log_k;
	public function __construct(){
		parent::__construct();
	}
	public function onInit(){
		$this->log_k=array(
			"dz_add"=>"对账操作",
			"fz_add"=>"分账操作",
		);
	}
	
	public function onDefault(){
		$sscy=M("sushe_user")->selectRow(array(
				"where"=>"userid=".M("login")->userid,
			));
		if($sscy){
			
			$where="   (userid=".M("login")->userid." or  sushe=".$sscy['sushe']."  ) "; 
		}else{
			$where="   userid=".M("login")->userid;
		}
		$url=APPINDEX."?m=koudai_order&a=dzlog";
		$start=get('per_page','i');
		$limit=20;
		$option=array(
			"where"=>$where,
			"order"=>"id DESC",
			"start"=>$start,
			"limit"=>$limit
		);
		$rscount=true;
		$data=M("koudai_dz_log")->select($option,$rscount);
		if($data){
			foreach($data as $v){
				$uids[]=$v['userid'];
			}
			$uids=array_unique($uids);
			$us=M("user")->getUserByIds($uids);
			foreach($data as $k=>$v){
				if($v['k']=='fz_add'){
					$fz=M("koudai_order_fz_product")->getRow("select sum(num) as num,sum(money) as money from ".table('koudai_order_fz_product')." where userid=".$v['userid']." AND order_id=".$v['order_id']." ");
					$v['money']=$fz['money'];
					$v['num']=$fz['num'];
					$v['status_name']='已分账';
				}elseif($v['k']=='dz_add'){
					$ps=M("koudai_order_product")->select(array(
						"where"=>"order_id=".$v['order_id']
					));
					$koudai_order=M("koudai_order")->selectRow("order_id=".$v['order_id']);
					$gznum=$thnum=$jsnum=0;
					$gzmoney=$thmoney=$jsmoney=0;
					foreach($ps as $p){
						$gznum+=$p['gznum'];
						$gzmoney+=$p['gznum']*$p['price'];
						$thnum+=$p['thnum'];
						$thmoney+=$p['thnum']*$p['price'];
						$jsnum+=$p['jsnum'];
						$jsmoney+=$p['jsnum']*$p['price'];	
					}
					
					$v['gznum']=$gznum;
					$v['gzmoney']=$gzmoney;
					$v['thnum']=$thnum;
					$v['thmoney']=$thmoney;
					$v['jsnum']=$jsnum;
					$v['jsmoney']=$jsmoney;
					if($koudai_order['dzpay']){
						$v['status_name']='已支付';
					}else{
						$v['status_name']='未支付';
					}
				}
				$v['k_title']=$this->log_k[$v['k']];
				$v['user']=$us[$v['userid']];
				$v['admin']=M("koudai_admin")->selectRow("adminid=".$v['adminid']);
				$data[$k]=$v;
			}
			
		}
	 
		$this->smarty->assign(array(
			"data"=>$data	
		));
		$this->smarty->display("koudai_dz_log/index.html");
	}
	
	public function getDz(){
		
	}
	
	public function getFz(){
		
	}
	
}

?>